<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关联设备ID" prop="equipmentId">
        <el-input
          v-model="queryParams.equipmentId"
          placeholder="请输入关联设备ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="保养计划名称" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入保养计划名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="频率值" prop="frequencyValue">
        <el-input
          v-model="queryParams.frequencyValue"
          placeholder="请输入频率值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下次保养到期时间" prop="nextDueDate">
        <el-date-picker clearable
          v-model="queryParams.nextDueDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择下次保养到期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预计工时" prop="expectedDurationHours">
        <el-input
          v-model="queryParams.expectedDurationHours"
          placeholder="请输入预计工时"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否激活" prop="isActive">
        <el-input
          v-model="queryParams.isActive"
          placeholder="请输入是否激活"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人ID" prop="createdBy">
        <el-input
          v-model="queryParams.createdBy"
          placeholder="请输入创建人ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最后更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择最后更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['equipment-service:plans:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['equipment-service:plans:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['equipment-service:plans:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['equipment-service:plans:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="plansList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="保养计划ID" align="center" prop="planId" />
      <el-table-column label="关联设备ID" align="center" prop="equipmentId" />
      <el-table-column label="保养计划名称" align="center" prop="planName" />
      <el-table-column label="计划类型：定期/状态保养/故障维修" align="center" prop="planType" />
      <el-table-column label="频率类型" align="center" prop="frequencyType" />
      <el-table-column label="频率值" align="center" prop="frequencyValue" />
      <el-table-column label="下次保养到期时间" align="center" prop="nextDueDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.nextDueDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计工时" align="center" prop="expectedDurationHours" />
      <el-table-column label="是否激活" align="center" prop="isActive" />
      <el-table-column label="创建人ID" align="center" prop="createdBy" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后更新时间" align="center" prop="updatedAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['equipment-service:plans:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['equipment-service:plans:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备保养计划对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="plansRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="关联设备ID" prop="equipmentId">
          <el-input v-model="form.equipmentId" placeholder="请输入关联设备ID" />
        </el-form-item>
        <el-form-item label="保养计划名称" prop="planName">
          <el-input v-model="form.planName" placeholder="请输入保养计划名称" />
        </el-form-item>
        <!-- 计划类型下拉框 -->
        <el-form-item label="计划类型" prop="planType">
          <el-select v-model="form.planType" placeholder="请选择计划类型" clearable>
            <el-option
                v-for="(item, index) in planTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="频率类型" prop="frequencyType">
          <el-select v-model="form.frequencyType" placeholder="请输入频率类型" clearable>
            <el-option
                v-for="(item, index) in frequencyTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
<!--        <el-form-item label="频率类型" prop="frequencyType">-->
<!--          <el-input v-model="form.frequencyType" placeholder="请输入频率类型" />-->
<!--        </el-form-item>-->
        <el-form-item label="频率值" prop="frequencyValue">
          <el-input v-model="form.frequencyValue" placeholder="请输入频率值" />
        </el-form-item>
        <el-form-item label="下次保养到期时间" prop="nextDueDate">
          <el-date-picker clearable
            v-model="form.nextDueDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择下次保养到期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预计工时" prop="expectedDurationHours">
          <el-input v-model="form.expectedDurationHours" placeholder="请输入预计工时" />
        </el-form-item>
        <el-form-item label="是否激活" prop="isActive">
          <el-input v-model="form.isActive" placeholder="请输入是否激活" />
        </el-form-item>
        <el-form-item label="创建人ID" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入创建人ID" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择最后更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Plans">
import { listPlans, getPlans, delPlans, addPlans, updatePlans } from "@/api/equipment-service/plans"

const { proxy } = getCurrentInstance()

const plansList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const planTypeOptions=[
      { label: '定期保养', value: 'Scheduled' },
{ label: '状态保养', value: 'ConditionBased' },
{ label: '故障维修', value: 'Breakdown' }
]


const frequencyTypeOptions=[
  { label: '每小时', value: 'Hours' },
  { label: '每日的', value: 'Daily' },
  { label: '每周的', value: 'Weekly' },
  { label: '每月的', value: 'Monthly' },
  { label: '每季度的', value: 'Quarterly' },
  { label: '每年的', value: 'Yearly' },

]


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    equipmentId: null,
    planName: null,
    planType: null,
    frequencyType: null,
    frequencyValue: null,
    nextDueDate: null,
    expectedDurationHours: null,
    isActive: null,
    createdBy: null,
    createdAt: null,
    updatedAt: null
  }

,
  rules: {
    equipmentId: [
      { required: true, message: "关联设备ID不能为空", trigger: "blur" }
    ],
    planName: [
      { required: true, message: "保养计划名称不能为空", trigger: "blur" }
    ],
    planType: [
      { required: true, message: "计划类型：定期/状态保养/故障维修不能为空", trigger: "change" }
    ],
    isActive: [
      { required: true, message: "是否激活不能为空", trigger: "blur" }
    ],
    createdBy: [
      { required: true, message: "创建人ID不能为空", trigger: "blur" }
    ],
    createdAt: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    updatedAt: [
      { required: true, message: "最后更新时间不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询设备保养计划列表 */
function getList() {
  loading.value = true
  listPlans(queryParams.value).then(response => {
    plansList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    planId: null,
    equipmentId: null,
    planName: null,
    planType: null,
    frequencyType: null,
    frequencyValue: null,
    nextDueDate: null,
    expectedDurationHours: null,
    isActive: null,
    createdBy: null,
    createdAt: null,
    updatedAt: null
  }
  proxy.resetForm("plansRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.planId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加设备保养计划"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _planId = row.planId || ids.value
  getPlans(_planId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改设备保养计划"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["plansRef"].validate(valid => {
    if (valid) {
      if (form.value.planId != null) {
        updatePlans(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addPlans(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _planIds = row.planId || ids.value
  proxy.$modal.confirm('是否确认删除设备保养计划编号为"' + _planIds + '"的数据项？').then(function() {
    return delPlans(_planIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('equipment-service/plans/export', {
    ...queryParams.value
  }, `plans_${new Date().getTime()}.xlsx`)
}

getList()
</script>
